package day27;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Set;

public class MapInputPanel extends JPanel {
	private JPanel panel;
	private JButton button;
	private JTextField address;
	private JTextField birthday;
	private JTextField irum;
	private JTextField id;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JLabel statusbar;

	/**
	 * Create the panel.
	 */
	public MapInputPanel() {
		setLayout(new BorderLayout(0, 0));
		add(getPanel(), BorderLayout.CENTER);
		add(getStatusbar(), BorderLayout.SOUTH);

	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(null);
			panel.add(getButton());
			panel.add(getAddress());
			panel.add(getBirthday());
			panel.add(getIrum());
			panel.add(getId());
			panel.add(getLabel());
			panel.add(getLabel_1());
			panel.add(getLabel_2());
			panel.add(getLabel_3());
		}
		return panel;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("입력");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Data d = new Data(id.getText(), irum.getText(),
							birthday.getText(), address.getText());
					
					Set<String> key = MapFrame.map.keySet();
					if(key.contains(id.getText())){
						statusbar.setText("키가 중복되었거나 오류 발생");
						return;
					}
					MapFrame.map.put(id.getText(), d);
					
					statusbar.setText("1건의 자료가 추가되었습니다. 총건수:" 
								+ MapFrame.map.size());
				
				}
			});
			button.setBounds(77, 116, 97, 23);
		}
		return button;
	}
	private JTextField getAddress() {
		if (address == null) {
			address = new JTextField();
			address.setColumns(10);
			address.setBounds(81, 85, 193, 21);
		}
		return address;
	}
	private JTextField getBirthday() {
		if (birthday == null) {
			birthday = new JTextField();
			birthday.setColumns(10);
			birthday.setBounds(81, 60, 93, 21);
		}
		return birthday;
	}
	private JTextField getIrum() {
		if (irum == null) {
			irum = new JTextField();
			irum.setColumns(10);
			irum.setBounds(81, 35, 116, 21);
		}
		return irum;
	}
	private JTextField getId() {
		if (id == null) {
			id = new JTextField();
			id.setColumns(10);
			id.setBounds(81, 10, 70, 21);
		}
		return id;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("아이디");
			label.setBounds(12, 13, 57, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("성명");
			label_1.setBounds(12, 38, 57, 15);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("생년월일");
			label_2.setBounds(12, 63, 57, 15);
		}
		return label_2;
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("주소");
			label_3.setBounds(12, 88, 57, 15);
		}
		return label_3;
	}
	private JLabel getStatusbar() {
		if (statusbar == null) {
			statusbar = new JLabel("자료를 입력하세요...");
			statusbar.setOpaque(true);
			statusbar.setBackground(Color.LIGHT_GRAY);
		}
		return statusbar;
	}
}
